package com.cncf.service.impl;

import com.cncf.dao.ReportInfoMapper;
import com.cncf.entity.ReportInfo;
import com.cncf.entity.ReportInfoExample;
import com.cncf.entity.ReportInfoWithBLOBs;
import com.cncf.response.ResponseData;
import com.cncf.service.ReportInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zj
 */
@Service
public class ReportInfoServiceImp implements ReportInfoService {
    @Autowired
    private ReportInfoMapper reportInfoMapper;

    @Override
    public ResponseData<ReportInfoWithBLOBs> selectReportInfoWithBLOBsById(String id) {
        ResponseData<ReportInfoWithBLOBs> responseData = new ResponseData<>();
        ReportInfoWithBLOBs reportInfoWithBLOBs = reportInfoMapper.selectByPrimaryKey(id);
        if (reportInfoWithBLOBs == null) {
            responseData.jsonFill(2, "无效的id", null);
            return responseData;
        }
        responseData.jsonFill(1, null, reportInfoWithBLOBs);
        return responseData;
    }

    @Override
    public ResponseData<List<ReportInfoWithBLOBs>> selectAllReportInfoWithBLOBsByPage(Integer page, Integer pageSize) {
        ResponseData<List<ReportInfoWithBLOBs>> responseData = new ResponseData<>();
        List<ReportInfoWithBLOBs> reportInfoWithBLOBsList;
        if (page == null || pageSize == null) {
            ReportInfoExample reportInfoExample = new ReportInfoExample();
            reportInfoWithBLOBsList = reportInfoMapper.selectByExampleWithBLOBs(reportInfoExample);
            responseData.jsonFill(1, null, reportInfoWithBLOBsList);
            return responseData;
        }
        int offset = page * pageSize;
        int limit = pageSize;
        reportInfoWithBLOBsList = reportInfoMapper.selectAllReportInfoWithBLOBsByPage(offset, limit);
        responseData.jsonFill(1, null, reportInfoWithBLOBsList);
        responseData.setCount(reportInfoWithBLOBsList.size());
        return responseData;
    }

    @Override
    public ResponseData<List<ReportInfo>> fuzzySearch(String pactNumber, String productName, String productUnit) {
        ResponseData<List<ReportInfo>> responseData = new ResponseData<>();
        ReportInfoExample reportInfoExample = new ReportInfoExample();
        ReportInfoExample.Criteria criteria = reportInfoExample.createCriteria();
        System.out.println("pactNumber(out):" + pactNumber);
        if (pactNumber != null) {

            //大小写转换
            String firstTwoChar=pactNumber.substring(0,2);
            if ("gn".equals(firstTwoChar)||"GN".equals(firstTwoChar)||"gN".equals(firstTwoChar)){
                pactNumber="Gn"+pactNumber.substring(2);
                System.out.println("111检测pactNumber:"+pactNumber);
            }
            String firstChar=pactNumber.substring(0,1);
            if ("N".equals(firstChar)){
                pactNumber="n"+pactNumber.substring(1);
                System.out.println("222检测pactNumber:"+pactNumber);
            }

            String searchText = "%" + pactNumber + "%";
            criteria.andPactNumberLike(searchText);
        }
        if (productName != null) {
            String searchText = "%" + productName + "%";
            criteria.andProductNameLike(searchText);
        }
        if (productUnit != null) {
            String searchText = "%" + productUnit + "%";
            criteria.andProductUnitLike(searchText);
        }
        List<ReportInfo> reportInfoList = reportInfoMapper.selectByExample(reportInfoExample);
        responseData.jsonFill(1, null, reportInfoList);
        return responseData;
    }
}
